package com.ruoyi.gams.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.gams.domain.GamsWindow;
import com.ruoyi.gams.mapper.GamsScoringMapper;
import com.ruoyi.gams.mapper.GamsUserQueueMapper;
import com.ruoyi.gams.mapper.GamsWindowMapper;
import com.ruoyi.gams.service.IGamsHomePageService;
import com.ruoyi.gams.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GamsIGamsHomePageServiceImpl implements IGamsHomePageService
{

    @Autowired
    private GamsUserQueueMapper userQueueMapper;

    @Autowired
    private GamsScoringMapper scoringMapper;

    @Autowired
    private GamsWindowMapper windowMapper;

    @Override
    public HomePageVO getHomePage()
    {
        HomePageVO vo = new HomePageVO();

        //当天取号
        Integer countByCreateTime = userQueueMapper.countByCreateTime();
        vo.setTodayNumberCount(countByCreateTime);

        //服务器配置
        ServerConfigVO serverConfigVO = getServerConfigVO();
        vo.setServerConfig(serverConfigVO);

        //近七天业务办理量
        List<BussinessVO> bussinessVOList = userQueueMapper.findBussinessIdsAndName();
        bussinessVOList.forEach(e ->
        {
            List<BussinessCountVO> bussinessCountList = userQueueMapper.countByBussinessIdAndLast7Days(e.getBussinessId());
            e.setBussinessCountList(bussinessCountList);
        });
        vo.setBussinessLast7DayList(bussinessVOList);

        //近七天员工满意度
        List<StaffScoringVO> scoringVOS = scoringMapper.findStaffIds();
        scoringVOS.forEach(e ->
        {
            List<ScoringVO> avgForLast7DaysScoring = scoringMapper.findAVGForLast7DaysScoring(e.getStaffId());
            e.setScoringList(avgForLast7DaysScoring);
        });
        vo.setStaffLast7DaysScoringList(scoringVOS);
        return vo;
    }

    @Override
    public JSONObject queueCount()
    {
        JSONObject result = new JSONObject();

        result.put("title", "排队信息统计图");
        String[] data = {"已取号", "已叫号", "排队中"};
        JSONObject legend = new JSONObject();
        legend.put("data", data);
        result.put("legend", legend);

        JSONObject xAxis = new JSONObject();
        List<GamsWindow> gamsWindows = windowMapper.selectGamsWindowList(new GamsWindow());
        List<String> names = gamsWindows.stream().map(GamsWindow::getName).collect(Collectors.toList());
        xAxis.put("data", names);
        result.put("xAxis", xAxis);


        List<CountResultVO> list = userQueueMapper.countByWindowAndBussiness();

        int[] data1 = {20, 30, 18, 18, 30, 22};
        int[] data2 = {5, 12, 8, 4, 6, 9};
        int[] data3 = {15, 18, 10, 10, 24, 13};
        for (int i = 0; i < 6; i++)
        {
            CountResultVO vo = list.get(i);
            data1[i] = vo.getCnt1();
            data2[i] = vo.getCnt2();
            data3[i] = vo.getCnt3();
        }


        JSONArray series = new JSONArray();

        JSONObject series1 = new JSONObject();
        series1.put("name", "已取号");
        series1.put("data", data1);
        series.add(series1);

        JSONObject series2 = new JSONObject();
        series2.put("name", "已叫号");
        series2.put("data", data2);
        series.add(series2);

        JSONObject series3 = new JSONObject();
        series3.put("name", "排队中");
        series3.put("data", data3);
        series.add(series3);

        result.put("series", series);


        //当天取号
        Integer countByCreateTime = userQueueMapper.countByCreateTime();
        result.put("countDay", countByCreateTime);

        //服务器配置
        ServerConfigVO serverConfigVO = getServerConfigVO();
        result.put("serverConfig", serverConfigVO);
        return result;
    }

    private ServerConfigVO getServerConfigVO()
    {
        ServerConfigVO serverConfig = new ServerConfigVO();
        serverConfig.setDatabasePort("3306");
        serverConfig.setDatabaseUrl("120.25.158.89");
        serverConfig.setServerUrl("https://gams.quickiface.com/");
        serverConfig.setServerPort("8084");
        serverConfig.setUsername("admin");
        serverConfig.setPassword("admin123");
        return serverConfig;
    }
}
